from qturboframe.lib.report.log import logger
from qturboframe.lib.webdriver.exceptions import SeleniumError
from BladeFerm.hui_app.hui_lib.link_api import LinkApi as HuiLinkApi
from .form import Form

class LinkApi(HuiLinkApi):
    '''功能权限菜单关联API弹出框多个表单编辑对象控件操作'''
    
    def set_value(self, value):
        '''
        填值操作(单项)

        :Args:
         - value: 需要填入表单的字典集合

        :Example:
         - 例: linkapi.set_value(value)
        '''
        set_li = self._element.find_elements_by_attribute('css selector', 'li', timeout=self.timeout)[-1]
        form = Form({'css selector': 'form.h-form'}, set_li, self.timeout)
        form.auto_fill(value)

    def delete_api(self, form_dict):
        '''
        删除api操作

        :Args:
         - form_dict: 需要删除的字典集合

        :Example:
         - 例: linkapi.delete_api(form_dict)
        '''
        key_list = list(form_dict.keys())
        row = 0
        for key in key_list:
            if form_dict.get(key) == '-':
                form_dict.pop(key)
            else:
                row = len(form_dict.get(key).split(','))
        if not form_dict:
            raise AttributeError('表格检查条件为空！')
        for index in range(0, row):
            new_dic = dict()
            for key in form_dict:
                new_dic[key] = form_dict.get(key).split(',')[index]
            delete_li = self.__get_cell(new_dic)
            # 点击删除图标
            logger.debug('删除api')
            delete_li.find_element_by_attribute('css selector', 'span i.h-icon').secure_click()

    def check_cells_exist(self, form_dict):
        '''
        检查api行数据是否存在

        :Args:
         - form_dict: 需要检查的字典集合

        :Example:
         - 例: linkapi.check_cells_exist(form_dict)
        '''
        key_list = list(form_dict.keys())
        row = 0
        for key in key_list:
            if form_dict.get(key) == '-':
                form_dict.pop(key)
            else:
                row = len(form_dict.get(key).split(','))
        if not form_dict:
            raise AttributeError('表格检查条件为空！')
        for index in range(0, row):
            new_dic = dict()
            for key in form_dict:
                new_dic[key] = form_dict.get(key).split(',')[index]
            self.__get_cell(new_dic)
        logger.info('数据校验已通过')

    def edit_api(self, form_dict):
        '''
        修改api操作(单项)

        :Args:
         - form_dict: 需要修改的字典集合({'修改前-xxx': 'xxx', '修改后-xxx': 'yyy'})

        :Example:
         - 例: linkapi.edit_api(form_dict)
        '''
        key_list = list(form_dict.keys())
        before_dict = dict()
        after_dict = dict()
        for key in key_list:
            if form_dict.get(key) == '-':
                form_dict.pop(key)
            elif '修改前-' in key:
                before_dict[key.split('-')[1]] = form_dict.get(key)
            elif '修改后-' in key:
                after_dict[key.split('-')[1]] = form_dict.get(key)
        li = self.__get_cell(before_dict)
        form = Form({'css selector': 'form.h-form'}, li, self.timeout)
        form.auto_fill(after_dict)

    def __get_cell(self, form_dict):
        key_list = list(form_dict.keys())
        for key in key_list:
            if form_dict.get(key) == '-':
                form_dict.pop(key)
        form_lis = self._element.find_elements_by_attribute('css selector', 'li', timeout=self.timeout)
        index = 0
        for li in form_lis:
            index += 1
            row = 0
            for key in form_dict:
                key_div = li.find_element_by_attribute('css selector', 'div.h-form-item[title="{}"]'.format(key), timeout=self.timeout)
                if key_div.find_element_by_attribute('css selector', 'div.h-input-wrapper input', timeout=self.timeout).get_attribute('value') != form_dict.get(key):
                    if index == len(form_lis):
                        raise SeleniumError('检查的数据不存在')
                else:
                    row += 1
                    logger.debug('匹配到{}的值:{}'.format(key, form_dict.get(key)))
            if row == len(form_dict.keys()):
                logger.debug('该行数据已验证存在')
                return li